/**
 * 
 */

function getRequest() {

	if (window.XMLHttpRequest) {

		return (new XMLHttpRequest());

	} else if (window.ActiveXObject) {

		return (new ActiveXObject("Microsoft.XMLHTTP"));

	} else {

		return (null);
	}

}

function sendRequest() {

	var request = getRequest();

	request.onreadystatechange = function() {
		handleResponse(request);
	};

	request.open("GET", "message.html", true);

	request.send(null);

}

function ajaxAlert(address) {

	var request = getRequest();

	request.onreadystatechange = function() {
		handleJSPResponse(request);
	};

	request.open("GET", address, true);

	request.send(null);

}

function ajaxResult(address, resultRegion) {
	var request = getRequest();
	request.onreadystatechange = function() {
		showResponseText(request, resultRegion);
	};
	request.open("GET", address, true);
	request.send(null);
}

function showResponseText(request, resultRegion) {
	if ((request.readyState == 4) && (request.status == 200)) {
		htmlInsert(resultRegion, request.responseText);
	}
}
function htmlInsert(id, htmlData) {
	document.getElementById(id).innerHTML = htmlData;
}

function handleResponse(request) {

	if (request.readyState == 4) {

		alert(request.responseText);

	}

}

function handleJSPResponse(request) {

	if ((request.readyState == 4) && (request.status == 200)) {

		alert(request.responseText);

	}

}

function getValue(id) {
	  return(escape(document.getElementById(id).value));
	}

function showMes(input, resultRegion){
	
	var baseAddr = "http://localhost:8080/Ajax/HelloServlet";
	
	var data = "name="+getValue(input);
	
	var address = baseAddr +"?"+data;
	
	ajaxResult(address, resultRegion);
	
	
}

function ajaxResultPost(address, data, resultRegion) {
	  var request = getRequest();
	  request.onreadystatechange = 
	    function() { showResponseText(request, 
	                                  resultRegion); };
	  request.open("POST", address, true);
	  request.setRequestHeader("Content-Type", 
	                           "application/x-www-form-urlencoded");
	  request.send(data);
	}


function showPOSTMes(input, resultRegion){
	
	var address = "/Ajax/HelloServlet";
	
	var data = "name="+getValue(input);
	
	
	
	ajaxResultPost(address, data, resultRegion);
	
	
}
